#define BOOST_TEST_MODULE MySuiteTest
#include <boost/test/included/unit_test.hpp>

#include <iostream>

#include "frreport_comm.h"
#include "frct_comm.h"
#include "frservice_comm.h"
#include "config.h"
#include "frmem_stat.h"
#include "frmem.h"
#include "pop3.h"

/*
*   pop3  私有信息
*/
typedef struct __st_L7session_pop3_private
{
    unsigned long ul_seq;                             //序列号，1次req和response时，使用同一个序列号
    L4_transaction st_l4_trans;                       //数据
    L7_transaction st_l7_trans;                       //数据
    int i_flag_data_type;                             //1: FRLOGIN_START 2:FRLOGIN_SUCC 3:FRLOGIN_FAIL 4 FRLOGOUT_START 5 FRLOGOUT_END 0 : other
    char *p_header_name;
    Frreport_body_data *p_st_body_data_req;
    Frreport_body_data *p_st_body_data_resp;
    Frlist_header *p_lst_comm;
    Frlist_header *p_lst_req;
    char *p_user_name;
    struct timeval st_login_timestamp;
    char arr_session_id[APM_UUID_SIZE];
    int i_sign;                                       //标志位 0||1, 0:需要resp解析 1:不需要resp解析
    int i_flag_end;                                   //标志位 0||1, 0:req为PASS 1:req不为PASS
    int i_flag_response;                              //标志位 0||1, 0:response的code和string不必上报 1：需要上报
}L7session_pop3_private;

L7session_pop3_private* __pop3_private_create(void);
int __req_data_analysis(Conntrack *p_st_conn, Apm_packet *p_st_packet, Frhash_header *p_st_hash_report, L7_session *p_st_session);

//__req_data_analysis
/* test_suite1 start */
BOOST_AUTO_TEST_SUITE(test_suite1)
int i_ret = 0;
Conntrack *p_st_conn = NULL;
Apm_packet *p_st_packet = NULL;
Frhash_header *p_st_hash_report = NULL;
L7_session *p_st_session = NULL;
// test_case1 in test_suite1
BOOST_AUTO_TEST_CASE(test_case1)
{
    fr_slab_init(102400, "ut_test");
    i_ret = __req_data_analysis(p_st_conn, p_st_packet, p_st_hash_report, p_st_session);
    BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
}
// test_case2 in test_suite1
BOOST_AUTO_TEST_CASE(test_case2)
{
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    assert(p_st_conn);
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    assert(p_st_packet);
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("UIDL\r\n");
    assert(p_st_packet->p_payload);
    p_st_packet->ui_payload_len = 0;
    p_st_packet->st_timestamp.tv_sec = 11;
    p_st_packet->st_timestamp.tv_usec = 22;

    p_st_hash_report = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    assert(p_st_hash_report);
    memset(p_st_hash_report, 0x00, sizeof(Frhash_header));

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    assert(p_st_session);
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    assert(p_st_session->p_session_private);
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 0;

    i_ret = __req_data_analysis(p_st_conn, p_st_packet, p_st_hash_report, p_st_session);

    BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 0);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes, 0);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec, 11);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec, 22);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec, 0);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec, 0);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frfree(p_st_hash_report);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);
}
// test_case3 in test_suite1
BOOST_AUTO_TEST_CASE(test_case3)
{
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    assert(p_st_conn);
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    assert(p_st_packet);
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("PASS zxd881104\r\n");
    assert(p_st_packet->p_payload);
    p_st_packet->ui_payload_len = 16;
    p_st_packet->st_timestamp.tv_sec = 11;
    p_st_packet->st_timestamp.tv_usec = 22;

    p_st_hash_report = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    assert(p_st_hash_report);
    memset(p_st_hash_report, 0x00, sizeof(Frhash_header));

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    assert(p_st_session);
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    assert(p_st_session->p_session_private);
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 0;

    i_ret = __req_data_analysis(p_st_conn, p_st_packet, p_st_hash_report, p_st_session);

    BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_TODO);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 0);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes, 16);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec, 11);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec, 22);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec, 11);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec, 22);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frfree(p_st_hash_report);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);
}
// test_case4 in test_suite1
BOOST_AUTO_TEST_CASE(test_case4)
{
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    assert(p_st_conn);
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    assert(p_st_packet);
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("UIDL\r\n");
    assert(p_st_packet->p_payload);
    p_st_packet->ui_payload_len = 6;
    p_st_packet->st_timestamp.tv_sec = 11;
    p_st_packet->st_timestamp.tv_usec = 22;

    p_st_hash_report = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    assert(p_st_hash_report);
    memset(p_st_hash_report, 0x00, sizeof(Frhash_header));

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    assert(p_st_session);
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    assert(p_st_session->p_session_private);
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 0;

    i_ret = __req_data_analysis(p_st_conn, p_st_packet, p_st_hash_report, p_st_session);

    BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 0);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes, 6);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec, 11);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec, 22);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec, 11);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec, 22);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frfree(p_st_hash_report);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);
}
// test_case5 in test_suite1
BOOST_AUTO_TEST_CASE(test_case5)
{
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    assert(p_st_conn);
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    assert(p_st_packet);
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("USER zhengxd@broada.com\r\n");
    assert(p_st_packet->p_payload);
    p_st_packet->ui_payload_len = 25;
    p_st_packet->st_timestamp.tv_sec = 11;
    p_st_packet->st_timestamp.tv_usec = 22;

    p_st_hash_report = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    assert(p_st_hash_report);
    memset(p_st_hash_report, 0x00, sizeof(Frhash_header));

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    assert(p_st_session);
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    assert(p_st_session->p_session_private);
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 0;

    i_ret = __req_data_analysis(p_st_conn, p_st_packet, p_st_hash_report, p_st_session);

    BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 1);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes, 25);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec, 11);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec, 22);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec, 11);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec, 22);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frfree(p_st_hash_report);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);
}
// test_case6 in test_suite1
BOOST_AUTO_TEST_CASE(test_case6)
{
    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    assert(p_st_conn);
    memset(p_st_conn, 0x00, sizeof(Conntrack));
    p_st_conn->flg_direct_known = FR_DIRECT_KNOWN;

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    assert(p_st_packet);
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("QUIT\r\n");
    assert(p_st_packet->p_payload);
    p_st_packet->ui_payload_len = 6;
    p_st_packet->st_timestamp.tv_sec = 11;
    p_st_packet->st_timestamp.tv_usec = 22;

    p_st_hash_report = (Frhash_header *)frmalloc(sizeof(Frhash_header));
    assert(p_st_hash_report);
    memset(p_st_hash_report, 0x00, sizeof(Frhash_header));

    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    assert(p_st_session);
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_st_session->p_session_private = __pop3_private_create();
    assert(p_st_session->p_session_private);
    ((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type = 0;

    i_ret = __req_data_analysis(p_st_conn, p_st_packet, p_st_hash_report, p_st_session);

    BOOST_REQUIRE_EQUAL(i_ret, APM_L7_ANALYZE_SET_REQ(APM_L7_ANALYZE_SUCC));
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->i_flag_data_type, 4);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.l_request_bytes, 6);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_sec, 11);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time_first_byte.tv_usec, 22);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_sec, 11);
    BOOST_REQUIRE_EQUAL(((L7session_pop3_private *)(p_st_session->p_session_private))->st_l4_trans.st_request_time.tv_usec, 22);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);
    frfree(p_st_hash_report);
    pop3_session_destroy(p_st_session->p_session_private);
    frfree(p_st_session);

    fr_slab_destroy();
}
BOOST_AUTO_TEST_SUITE_END()
/* test_suite1 end */